package com.zqf.testjzvideoplayer.jzvideo;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewParent;
import android.widget.FrameLayout;

import androidx.recyclerview.widget.RecyclerView;

import com.zqf.testjzvideoplayer.R;

import org.jetbrains.annotations.NotNull;

import java.util.List;

import cn.jzvd.Jzvd;

public class RecycleViewAdapterJava extends RecyclerView.Adapter<RecycleViewAdapterJava.VHolder> {
    private final Context context;
    private OnVideoClick onVideoClick;
    private final List<String> list;

    public RecycleViewAdapterJava(Context context, List<String> list) {
        this.context = context;
        this.list = list;
    }

    public void setOnVideoClick(OnVideoClick onVideoClick) {
        this.onVideoClick = onVideoClick;
    }

    @NotNull
    @Override
    public VHolder onCreateViewHolder(@NotNull ViewGroup parent, int viewType) {
        return new VHolder(LayoutInflater.from(context).inflate(R.layout.item_layout, parent, false));
    }

    @Override
    public void onBindViewHolder(@NotNull VHolder holder, int position) {
        JzVdStdRv jzvdStdRv;
        if (JzVdStdRv.CURRENT_JZVD != null && DataSource.positionInList == position) {
            ViewParent parent = JzVdStdRv.CURRENT_JZVD.getParent();
            if (parent != null) {
                ((ViewGroup) parent).removeView(JzVdStdRv.CURRENT_JZVD);
            }
            holder.container.removeAllViews();
            holder.container.addView(JzVdStdRv.CURRENT_JZVD, new FrameLayout.LayoutParams(
                    ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
            jzvdStdRv = (JzVdStdRv) JzVdStdRv.CURRENT_JZVD;
        } else {
            if (holder.container.getChildCount() == 0) {
                jzvdStdRv = new JzVdStdRv(holder.container.getContext());
                holder.container.addView(jzvdStdRv,
                        new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                                ViewGroup.LayoutParams.MATCH_PARENT));
            } else {
                jzvdStdRv = (JzVdStdRv) holder.container.getChildAt(0);
            }
            jzvdStdRv.setUp(list.get(position), "测试", Jzvd.SCREEN_NORMAL);
        }
        jzvdStdRv.setId(R.id.jzvdplayer);
        jzvdStdRv.setAtList(true);
        jzvdStdRv.setClickUi(new JzVdStdRv.ClickUi() {
            @Override
            public void onClickUiToggle() {
                DataSource.positionInList = position;
                jzvdStdRv.setAtList(false);
                ViewAttr attr = new ViewAttr();
                int[] location = new int[2];
                holder.container.getLocationInWindow(location);
                attr.setX(location[0]);
                attr.setY(location[1]);
                attr.setWidth(holder.container.getMeasuredWidth());
                attr.setHeight(holder.container.getMeasuredHeight());
                if (onVideoClick != null) onVideoClick.videoClick(holder.container, attr, position);
                jzvdStdRv.setClickUi(null);
            }

            @Override
            public void onClickStart() {
                DataSource.positionInList = position;
            }
        });
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    public interface OnVideoClick {
        void videoClick(ViewGroup focusView, ViewAttr viewAttr, int position);
    }

    static class VHolder extends RecyclerView.ViewHolder {
        FrameLayout container;

        public VHolder(View itemView) {
            super(itemView);
            container = itemView.findViewById(R.id.surface_container);
        }
    }
}
